<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace backend\models;

use Yii;
use yii\base\Exception;
use yii\base\Model;
use yii\web\UploadedFile;

class UploadFormDjr extends Model
{

    public $uploadedFiles;

    public function scenarios()
    {
        return [
            'files' => ['uploadedFiles'],
            'default' => ['uploadedFiles'],
        ];
    }

    public function rules()
    {
        return [
            [['uploadedFiles'], 'file', 'skipOnEmpty' => true, 'extensions' => 'png,jpg,jpeg,gif', 'maxFiles' => 10, 'checkExtensionByMimeType' => false, 'on' => ['default']],
            [['uploadedFiles'], 'file', 'skipOnEmpty' => true, 'extensions' => 'bmp,png,jpeg,jpg,doc,docx,xls,xlsx,txt,pdf,ppt,pptx', 'checkExtensionByMimeType' => false, 'on' => ['files']],
        ];
    }

    /**
     * @param $model
     * @param $basepath
     * @param $uploadedFiles
     * @return bool|string
     * @ai 添加项目附件、会员头像、会员认证相关照片，其他功能点请在此处添加。
     *
     */
    public function upload($model, $basepath, $uploadedFiles, $multiple = false)
    {
        $this->uploadedFiles = UploadedFile::getInstances($model, $uploadedFiles);
        $path = $basepath . '/' . date('Ymd', time()) . '/';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        if ($this->validate()) {
            foreach ($this->uploadedFiles as $key => $file) {
                $file_name = $path . uniqid(time(), false) . '.' . $file->extension;
                $file->saveAs($file_name);
                if ($multiple) {
                    $file_names[] = Yii::$app->request->getHostInfo() . '/' . $file_name;
                    return $file_names;
                } else {
                    return Yii::$app->request->getHostInfo() . '/' . $file_name;
                }
            }
        } else {
            return serialize($this->getErrors());
        }
    }


    /**
     * @后台上传项目附件
     * @param $model
     * @param $basepath
     * @param $uploadedFiles
     * @param bool $multiple
     * @return array|string
     */
    public function uploadFile($model, $basepath, $uploadedFiles, $multiple = false)
    {
        $this->uploadedFiles = UploadedFile::getInstance($model, $uploadedFiles);
//        Yii::$app->session->addFlash('danger', '创建实例：' . serialize($this->uploadedFiles));
        $path = $basepath . '/' . date('Ymd', time()) . '/';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }

        if ($this->validate()) {
            $file_name = $path . uniqid(time(), false) . '.' . $this->uploadedFiles->extension;
            if ($this->uploadedFiles) {
                try {
                    $result = $this->uploadedFiles->saveAs($file_name);
//                    Yii::$app->session->addFlash('info', '错误：' . serialize($this->uploadedFiles->getHasError()) . serialize($result));
                } catch (Exception $e) {
                    Yii::$app->session->addFlash('danger', '异常：' . $e->getMessage());
                }
//                Yii::$app->session->addFlash('warning', Yii::$app->request->getHostInfo() . '/' . $file_name . '#' . $this->uploadedFiles->name);
                return Yii::$app->request->getHostInfo() . '/' . $file_name . '#' . $this->uploadedFiles->name;
            } else {
                Yii::$app->session->addFlash('warning', '实例为空');
                return false;
            }
        } else {
            Yii::$app->session->addFlash('danger', '文件不符合要求：' . serialize($this->getErrors()));
        }
    }

    public function uploadImage($model, $basepath, $uploadedFiles, $multiple = false)
    {
        $this->uploadedFiles = UploadedFile::getInstances($model, $uploadedFiles);
        Yii::$app->session->addFlash('info', '创建实例：' . serialize($this->uploadedFiles));
        $path = $basepath . '/' . date('Ymd', time()) . '/';
        if (!is_dir($path)) {
            mkdir($path, 0777, true);
        }
        if ($this->validate()) {
            foreach ($this->uploadedFiles as $key => $file) {
                $file_name = $path . uniqid(time(), false) . '.' . $file->extension;
                try {
                    $file->saveAs($file_name);
                } catch (Exception $e) {
                    Yii::$app->session->addFlash('info', '异常：' . $e->getMessage());
                }
                if ($multiple) {
                    $file_names[] = $file_name;
                    return $file_names;
                } else {
                    return $file_name;
                }
            }
        } else {
            Yii::$app->session->addFlash('info', '文件不符合要求：' . serialize($this->getErrors()));
            return $this->getErrors();
        }
    }

}
